package cn.admobiletop.adsuyi.adapter.toutiao;

import android.text.TextUtils;

import java.util.Arrays;
import java.util.List;

import cn.admobiletop.adsuyi.ADSuyiSdk;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterIniter;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterIniterExtParams;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.data.ADSuyiAdType;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatform;
import cn.admobiletop.adsuyi.adapter.toutiao.loader.BannerAdLoader;
import cn.admobiletop.adsuyi.adapter.toutiao.loader.FullScreenVodAdLoader;
import cn.admobiletop.adsuyi.adapter.toutiao.loader.InterstitialAdLoader;
import cn.admobiletop.adsuyi.adapter.toutiao.loader.NativeAdLoader;
import cn.admobiletop.adsuyi.adapter.toutiao.loader.RewardVodAdLoader;
import cn.admobiletop.adsuyi.adapter.toutiao.loader.SplashAdLoader;
import cn.admobiletop.adsuyi.adapter.toutiao.manager.TTAdInitManager;
import cn.admobiletop.adsuyi.adapter.toutiao.util.TTAdUtil;
import cn.admobiletop.adsuyi.config.ADSuyiInitConfig;
import cn.admobiletop.adsuyi.util.ADSuyiLogUtil;

/**
 * @author ciba
 * @description 头条平台初始化类
 * @date 2020/3/25
 */
public class ADSuyiIniter implements ADSuyiAdapterIniter {
    private static final String[] SUPPORT_ADSUYI_SDK_VERSIONS = {"3.4.0.10141"};
    public static final String PLATFORM = "toutiao";
    private static boolean inited = false;

    @Override
    public void init(ADSuyiPlatform platform, ADSuyiAdapterIniterExtParams extParams) {
        if (!inited && platform != null && platform.check()) {
            // 头条传入appid为null或空字符串不会影响初始化，但是会导致请求广告过程中直接崩溃，所以需要做非空判断
            if (TextUtils.isEmpty(platform.getAppId())) {
                return;
            }
            ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
            // 判断用户是否过滤第三方平台的问题广告
            if (config != null && config.isFilterThirdQuestion()) {
                // 低版本设备存在系统webview的问题，不参与头条渠道初始化ßß
                if (TTAdUtil.withoutLowSystem()) {
                    ADSuyiLogUtil.d("toutiao 头条初始化过滤，安卓5.1及以下设备存在系统webview的问题");
                    return;
                }
            }
            TTAdInitManager.getInstance().init(platform.getAppId(), platform.getAppKey());
            inited = true;
        }
    }

    @Override
    public boolean inited() {
        return inited;
    }

    @Override
    public ADSuyiAdapterLoader getSuyiAdapterLoader(String adType) {
        if (ADSuyiAdType.TYPE_SPLASH.equals(adType)) {
            return new SplashAdLoader();
        } else if (ADSuyiAdType.TYPE_BANNER.equals(adType)) {
            return new BannerAdLoader();
        } else if (ADSuyiAdType.TYPE_FLOW.equals(adType)) {
            return new NativeAdLoader();
        } else if (ADSuyiAdType.TYPE_REWARD_VOD.equals(adType)) {
            return new RewardVodAdLoader();
        } else if (ADSuyiAdType.TYPE_FULLSCREEN_VOD.equals(adType)) {
            return new FullScreenVodAdLoader();
        } else if (ADSuyiAdType.TYPE_INTERSTITIAL.equals(adType)) {
            return new InterstitialAdLoader();
        }
        return null;
    }

    @Override
    public List<String> getSupportADSuyiSdkVersions() {
        return Arrays.asList(SUPPORT_ADSUYI_SDK_VERSIONS);
    }

    @Override
    public String getAdapterVersion() {
        return BuildConfig.VERSION_NAME;
    }

}
